// Copyright 2012 Square, Inc.
package com.squareup.timessquare;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * CalendarRowView
 */
public class CalendarRowView extends ComponentContainer implements Component.ClickedListener {
    private boolean isHeaderRow;
    private MonthView.Listener listener;

    public CalendarRowView(Context context, AttrSet attrs) {
        super(context, attrs);
    }

    @Override
    public void addComponent(Component childComponent) {
        childComponent.setClickedListener(this);
        super.addComponent(childComponent);
    }

    /**
     * onLayout 暂时不能用
     * @param changed changed
     * @param left left
     * @param top top
     * @param right right
     * @param bottom bottom
     */
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        long start = System.currentTimeMillis();
        int cellHeight = bottom - top;
        int width = right - left;
        for (int c = 0, numChildren = getChildCount(); c < numChildren; c++) {
            final Component child = getComponentAt(c);
            int l1 = (c * width) / 7;
            int r1 = ((c + 1) * width) / 7;
            LayoutConfig layoutConfig = new LayoutConfig();
            layoutConfig.setMargins(l1, 0, r1, cellHeight);
            child.setLayoutConfig(layoutConfig);
        }
        Logr.debug("Row.onLayout %d ms", System.currentTimeMillis() - start);
    }

    /**
     * setIsHeaderRow
     * @param isHeaderRow isHeaderRow
     */
    public void setIsHeaderRow(boolean isHeaderRow) {
        this.isHeaderRow = isHeaderRow;
    }

    /**
     * setListener
     * @param listener listener
     */
    public void setListener(MonthView.Listener listener) {
        this.listener = listener;
    }

    /**
     * setDayViewAdapter
     * @param adapter adapter
     */
    public void setDayViewAdapter(DayViewAdapter adapter) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getComponentAt(i) instanceof CalendarCellView) {
                CalendarCellView cell = ((CalendarCellView) getComponentAt(i));
                cell.removeAllComponents();
                adapter.makeCellView(cell);
            }
        }
    }

    /**
     * setCellBackground
     * @param resId resId
     */
    public void setCellBackground(int resId) {
        for (int i = 0; i < getChildCount(); i++) {
            ShapeElement element = new ShapeElement();
            RgbColor color = new RgbColor(resId);
            element.setRgbColor(color);
            getComponentAt(i).setBackground(element);
        }
    }

    /**
     * setCellTextColor
     * @param resId resId
     */
    public void setCellTextColor(int resId) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getComponentAt(i) instanceof CalendarCellView) {
                ((CalendarCellView) getComponentAt(i)).getDayOfMonthTextView().setTextColor(new Color(resId));
            } else {
                ((Text) getComponentAt(i)).setTextColor(new Color(resId));
            }
        }
    }

    /**
     * setCellTextColor
     * @param colors colors
     */
    public void setCellTextColor(Color colors) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getComponentAt(i) instanceof CalendarCellView) {
                ((CalendarCellView) getComponentAt(i)).getDayOfMonthTextView().setTextColor(colors);
            } else {
                ((Text) getComponentAt(i)).setTextColor(colors);
            }
        }
    }

    /**
     * setTypeface
     * @param typeface typeface
     */
    public void setTypeface(Font typeface) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getComponentAt(i) instanceof CalendarCellView) {
                ((CalendarCellView) getComponentAt(i)).getDayOfMonthTextView().setFont(typeface);
            } else {
                ((Text) getComponentAt(i)).setFont(typeface);
            }
        }
    }

    @Override
    public void onClick(Component component) {
        if (listener != null) {
            listener.handleClick((MonthCellDescriptor) component.getTag());
        }
    }
}
